package com.example.android_wx.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.android_wx.R;
import com.example.android_wx.base.App;
import com.example.android_wx.base.BaseActivity;
import com.example.android_wx.base.GlideEngine;
import com.example.android_wx.fragment.MineFragment;
import com.example.android_wx.http.BaseCallBack;
import com.example.android_wx.http.HttpHelp;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.io.File;
import java.util.List;

public class Personalinformation extends BaseActivity {
    private RelativeLayout ChangeyourAvatar;
    private RelativeLayout Changenickname;
    private RelativeLayout Modifymobilephonenumber;
    private RelativeLayout Modifygender;
    private AppCompatImageView avatarImageView;
    private AppCompatTextView nicknameTextView;
    private AppCompatTextView modifyMobileTextView;
    private AppCompatTextView modifyGenderTextView;
    private AppCompatImageView pefImageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_personalinformation);
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initView() {
        ChangeyourAvatar = findViewById(R.id.ChangeyourAvatar);
        Changenickname = findViewById(R.id.Changenickname);
        Modifymobilephonenumber = findViewById(R.id.Modifymobilephonenumber);
        Modifygender = findViewById(R.id.Modifygender);
        avatarImageView = findViewById(R.id.avatarImageView);
        nicknameTextView = findViewById(R.id.nicknameTextView);
        modifyMobileTextView = findViewById(R.id.modifyMobileTextView);
        modifyGenderTextView = findViewById(R.id.modifyGenderTextView);
        pefImageView = findViewById(R.id.pefImageView);

    }

    @Override
    public void onResume() {
        super.onResume();

        //重新加载信息
        Glide.with(this).load(App.get().getMemberData().getMemberAvatar()).into(avatarImageView);
        modifyMobileTextView.setText(App.get().getMemberData().getMemberMobile());
        nicknameTextView.setText(App.get().getMemberData().getMemberNickname());

        if (App.get().getMemberData().getMemberGender() == 1) {
            modifyGenderTextView.setText("男");
        } else {
            modifyGenderTextView.setText("女");
        }



    }

    @Override
    public void initEvent() {


        pefImageView.setOnClickListener(v -> {
            finish();
        });

        //修改头像
        ChangeyourAvatar.setOnClickListener(v -> {
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .loadImageEngine(GlideEngine.createGlideEngine())
                    .forResult(new OnResultCallbackListener<LocalMedia>() {
                        @Override
                        public void onResult(List<LocalMedia> result) {
                            System.out.println(result.get(0).getPath());
                            // 结果回调
                            HttpHelp.get().ready("member", "modifyAvatar")
                                    .post(new File(result.get(0).getPath()), new BaseCallBack() {
                                        @Override
                                        public void onSuccess(String data) {
                                            App.get().getMemberData().setMemberAvatar(data);
                                            Glide.with(Personalinformation.this).load(data).into(avatarImageView);

                                        }

                                        @Override
                                        public void onFailure(String reason) {

                                        }
                                    });

                        }

                        @Override
                        public void onCancel() {

                        }
                    });


        });
        //修改昵称
        Changenickname.setOnClickListener(view -> startActivity(new Intent(this, ModifyNameActivity.class)));
        //修改手机号码
        Modifymobilephonenumber.setOnClickListener(view -> startActivity(new Intent(this, ModifyMobileActivity.class)));
        //修改性别
        Modifygender.setOnClickListener(view -> startActivity(new Intent(this, ModifyGenderActivity.class)));


        //加载信息
        Glide.with(this).load(App.get().getMemberData().getMemberAvatar()).into(avatarImageView);
        modifyMobileTextView.setText(App.get().getMemberData().getMemberMobile());
        nicknameTextView.setText(App.get().getMemberData().getMemberNickname());

        if (App.get().getMemberData().getMemberGender() == 1) {
            modifyGenderTextView.setText("男");
        } else {
            modifyGenderTextView.setText("女");
        }


    }
}